package com.itheima.ssm.dao;

import com.itheima.ssm.domain.Pemission;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IPermessionDao {

    /**
     * 根据roleid查询权限：查询的是当前role对应的那些权限，封装到list中
     * @param roleId
     * @return
     */
    @Select("select pemeission from permession where id in (select permessionId from role_permession where id = #{roleId})")
    public List<Pemission> findPermissionByRoleId(int roleId);


    /**
     * 查询所有权限
     * @return
     */
    @Select("select * from permession")
    public List<Pemission> findAll() throws Exception;

    /**
     * 添加权限
     * @param pemission
     * @throws Exception
     */
    @Insert("insert into permession(permessionName,url) values(#{permessionName},#{url})")
    public void save(Pemission pemission) throws Exception;


    /**
     * 通过id查找权限的详细信息
     * 注意通过中间表role_permession查询到role
     * role中封装了user的信息，所以我们还需要通过users_role中间表查询到userId
     * 然后根据此查询到user的信息
     * @param pemissionId
     * @return
     */
    @Select("select * from permession")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "roleName",column = "roleName"),
            @Result(property = "roleDesc",column = "roleDesc"),
            @Result(property = "userInfo",column = "id",javaType = java.util.List.class,many = @Many(select = "com.itheima.ssm.dao.IRoleDa0.findRoleByPermessionId")),
    })
    public Pemission findById(int pemissionId) throws Exception;
}
